<?php
namespace Admin\Controller;
use Admin\Model\GoodsTypeModel;

/**
 * 后台管理商品管理控制器
 * @author RickChan
 */
class GoodsController extends AdminController {
	public function index(){
		$typelist = M("GoodsType");
		$condition['status'] = 1;
        $goodstypelist = $typelist->where($condition)->select();

        $id = I('get.id',$goodstypelist[0]['id']);

		$status = I('status',null);
		$goodsname = I('goodsname');
		if(!empty($status) || $status == '0'){
			$map['status'] = $status;
		}else{
			$map['status'] = array('neq',-1);
		}
		if(!empty($goodsname)){
			$map['name'] = array('LIKE','%'.$goodsname.'%');
		}
		$map['type_id'] = $id;

        C('LIST_ROWS',20);
		$list = $this->lists('Goods',array($map),'sort desc,id desc');
		int_to_string($list);
		$this->assign('status',$status);
		$this->assign('_list', $list);
		$this->assign('typelist',$goodstypelist);
		$this->assign('id',$id);
		$this->meta_title = '商品管理';
		$this->display();
	}

	public function edit(){
		$id = I('get.id');
		empty($id) && $this->error('参数不能为空！');
		$data = M('Goods')->field(true)->find($id);

		$list = $this->getGoodstype();
		$this->assign('_typelist',$list);
		$this->assign('data',$data);
		$this->meta_title = '编辑商品';
		$this->display('editgoods');
	}

	public function createGoods(){
		$list = $this->getGoodstype();
		$this->assign('_typelist',$list);
		$this->assign('data',null);
		$this->meta_title = '新增商品';
		$this->display('addgoods');
	}

	public function writeGoods(){
		$res = D('Goods')->update();
		if(!$res){
		    $this->error(D('Goods')->getError());
        }else{
            $this->success($res['id']?'更新成功！':'新增成功！', U('index'));
        }
	}

	public function getGoodstype(){
		$GoodsType = M("GoodsType");
		$list = $GoodsType->where('status=1')->select();
		return $list;
	}

	public function changeStatus($method=null){
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',', $id) : $id;
		if (empty($id)){
			$this->error('请选择要操作的数据！');
		}
		$map['id'] = array('in',$id);
		switch (strtolower($method)) {
			case 'forbidgoods':
                $this->forbid('Goods', $map );
                break;
            case 'resumegoods':
                $this->resume('Goods', $map );
                break;
            case 'deletegoods':
                $this->delete('Goods', $map );
                break;
            default:
                $this->error('参数非法');
				break;
		}
	}

	public function goodsspec(){
		$gid = I('get.gid');
		empty($gid) && $this->error('参数不能为空！');

		$list = $this->lists('GoodsSpecification',array('goods_id'=>$gid,'status!=-1'),'id desc');
		int_to_string($list);
		$this->assign('_list', $list);

		$this->assign('gid',$gid);
		$this->meta_title = '规格管理';
		$this->display('goodsspec');
	}

	public function createSpec(){
		$gid = I('get.gid');
		$this->assign('gid',$gid);
		$this->assign('data',null);
		$this->meta_title = '新增规格';
		$this->display('editspec');
	}

	public function editspec(){
		$gid = I('get.gid');
		$id = I('get.id');
		empty($id) && $this->error('参数不能为空！');
		$data = M('GoodsSpecification')->field(true)->find($id);

		$this->assign('gid',$gid);
		$this->assign('data',$data);
		$this->meta_title = '编辑商品';
		$this->display('editspec');
	}

	public function writeSpec(){
		$gid = I('post.goods_id');
		$res = D('GoodsSpecification')->update();
		if(!$res){
		    $this->error(D('GoodsSpecification')->getError());
        }else{
            $this->success($res['id']?'更新成功！':'新增成功！', U('goodsspec?gid='.$gid));
        }
	}

	public function changeSpecStatus($method=null){
		$id = array_unique((array)I('id',0));
		$id = is_array($id) ? implode(',', $id) : $id;
		if (empty($id)){
			$this->error('请选择要操作的数据！');
		}
		$map['id'] = array('in',$id);
		switch (strtolower($method)) {
			case 'forbidspec':
                $this->forbid('GoodsSpecification', $map );
                break;
            case 'resumespec':
                $this->resume('GoodsSpecification', $map );
                break;
            case 'deletespec':
                $this->delete('GoodsSpecification', $map );
                break;
            default:
                $this->error('参数非法');
				break;
		}
	}
}